package com.s10.ecloud.web.action;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.s10.ecloud.web.Constants;
import com.s10.ecloud.web.model.EnterpriseUser;
import com.s10.ecloud.web.model.IndividualUser;
import com.s10.ecloud.web.model.User;
import com.s10.ecloud.web.service.IUserService;
import com.s10.ecloud.web.util.MD5;

/**
 * 个人用户，密码修改
 * 
 * @author Kevin
 */
public class A010102Action extends ActionSupport {

	@Resource
	private IUserService userService;

	@Action(value = "/toEditIndPassword", results = { @Result(name = "success", location = "/page/ind/editIndPasswordPage.jsp") })
	public String toEditPasswordPage() throws Exception {

		return this.SUCCESS;
	}

	@Action(value = "/updateIndPassword", results = {
			@Result(name = "success", location = "/page/ind/index.jsp"),
			@Result(name = "input", location = "/page/ind/editIndPasswordPage.jsp") })
	public String UpdatePassword() throws Exception {

		User user = (User) ActionContext.getContext().getSession()
				.get(Constants.SESSION_KEY_USER);

		user = userService.getUser(user);

		if (user == null
				|| !MD5.MD5Encode(this.getOldPassword()).equals(
						user.getPassword())) {

			this.addFieldError("oldPassword", "旧密码输入不正确，请重新输入");
			return this.INPUT;
		} else {
			// TODO 1.check

			// 更新新密码。
			user.setPassword(MD5.MD5Encode(this.getPassword()));
			java.sql.Date dateNow = new java.sql.Date(
					new java.util.Date().getTime());
			user.setUpdateDate(dateNow);

			user = userService.updateUser(user);
			// 更新session中的内容
			ActionContext.getContext().getSession()
					.put(Constants.SESSION_KEY_USER, user);
		}

		return this.SUCCESS;

	}

	private String oldPassword;

	private String password;

	private String comfirmPassword;

	public String getOldPassword() {
		return oldPassword;
	}

	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getComfirmPassword() {
		return comfirmPassword;
	}

	public void setComfirmPassword(String comfirmPassword) {
		this.comfirmPassword = comfirmPassword;
	}

}
